A frequent task in templating and controller development is to find what groups and organisations the current user is a member of. Kademi supports this through the Membership API.

There are two objects used to represent users

  • UserResource - an addressable object (ie with its own href)
  • ProfileBean - a lightweight object suitable for use when traversing large user lists.

The current user is represented in templating context with the $user variable. So you can retrieve their first name like this:

$user.firstName

You can get a ProfileBean from a UserResource with the profile property. Eg:

$user.profile

 

If you have a ProfileBean and need a UserResource you can look it up as a resource by its href:

#set( $theUser = $page.find("/users/$profile.userName" ))

 

To get the list of memberships you use the memberships property, which returns a MembershipList:

$user.memberships

That returns a list of MembershipBean, where each membership represents a link between a profile, a group and an organisation. The organisation is accessed through the org property which returns an OrgData object, which has properties and methods for accessing the organisation information and complete membership

The MembershipList can be filtered to get the list of memberships only for a certain group or organisation.

For example, to display the list of addresses of car dealerships the current user is a Dealer for you would use this:

<ul>
  #foreach( $membership in $user.memberships.filterByGroup("Dealer") )
  <li>$membership.org.address</li>
  #end
</ul>

The organisation representing the current Kademi account can be accessed through the orgData property of the WebsiteRootFolder. The current folder is in templating context as the $rootFolder variable. From the OrgData object you can lookup organisations inside it with the childOrgs() method, which returns an OrgDataList.

So to display all dealerships and the number of Dealers in them you might do this:

<table>
#foreach( $dealership in $rootFolder.orgData.childOrgs()  )
<tr>
  <td>$dealership.title</td>
  <td>$dealership.members("Dealer").size()</td>
</tr>
#end
</table>

 

Ask a question, or offer an answer